
package com.monlyu.bo;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.exception.MsgException;
import com.monlyu.util.DateUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.Mail;
import com.monlyu.vo.User;

/********************************************************************************
 * <b>功能描述☞</b><br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.bo.MailBo.java<br>
 * <b>开发日期☞</b>2010-2-25 <br>
 * 
 * @author zengjun
 * @since JDK 5
 * @version 1.0.8
 ********************************************************************************/
@Repository(value = "mailBo")
public class MailBo extends BaseBo {

    /**
     * 得到user已经发送邮件列表
     * 
     * @param user
     * @param page
     * @return
     */
    public Pages<Mail> getSendMails(User user, Page page){
        Criteria criteria = dao.initCriteria(Mail.class);
        criteria.add(Restrictions.eq("sender", user));
        criteria.add(Restrictions.eq("senderDelete", false));
        criteria.add(Restrictions.eq("senderRemove", false));
        criteria.addOrder(Order.desc("createDate"));
        return buildPages(page, criteria);
    }

    /**
     * 得到user删除邮件列表
     * 
     * @param user
     * @param page
     * @return
     */
    public Pages<Mail> getDeletedMails(User user, Page page){
        Criteria criteria = dao.initCriteria(Mail.class);
        criteria.add(Restrictions.eq("status", Mail.MAIL_STATUS_DELETE));
        criteria.add(Restrictions.or(Restrictions.and(Restrictions.eq("sender", user), Restrictions.eq(
            "senderDelete",
            false)), Restrictions.and(Restrictions.eq("receiver", user), Restrictions.eq("receiverDelete", false))));
        criteria.add(Restrictions.or(Restrictions.and(Restrictions.eq("sender", user), Restrictions.eq(
            "senderRemove",
            true)), Restrictions.and(Restrictions.eq("receiver", user), Restrictions.eq("receiverRemove", true))));
        criteria.addOrder(Order.desc("createDate"));
        return buildPages(page, criteria);
    }

    /**
     * 得到user接收到的邮件列表
     * 
     * @param user
     * @param page
     * @return
     */
    public Pages<Mail> getReceivedMails(User user, Page page){
        Criteria criteria = dao.initCriteria(Mail.class);
        criteria.add(Restrictions.eq("receiver", user));
        criteria.addOrder(Order.desc("createDate"));
        criteria.add(Restrictions.eq("receiverDelete", false));
        criteria.add(Restrictions.eq("receiverRemove", false));
        return buildPages(page, criteria);
    }

    public List<User> findUsers(){
        return dao.find(User.class);
    }

    @SuppressWarnings("unchecked")
    public List<User> findOtherUsers(){
        return dao.find("from com.monlyu.vo.User u where u.enable=true and u.id!=?", WebOptUtil.curUser().getId());
    }

    public void sendMail(Mail mail, Long[] receiverIds){
        mail.setSender(WebOptUtil.curUser());
        mail.setStatus(Mail.MAIL_STATUS_NEW);
        mail.setCreateDate(DateUtil.now());
        for (Long receiverId : receiverIds) {
            User receiver = dao.get(User.class, receiverId);
            if (receiver.getId().equals(WebOptUtil.curUser().getId())) {
                throw new MsgException("发件人和收件人不能相同！");
            }
            Mail m = new Mail();
            m.setContent(mail.getContent());
            m.setCreateDate(mail.getCreateDate());
            m.setReceiver(receiver);
            m.setStatus(mail.getStatus());
            m.setTitle(mail.getTitle());
            m.setSender(mail.getSender());
            dao.insertOrUpdate(m);
        }
    }

    /**
     * 发送审核消息
     */
    public void sendMail(String title, String content, Long userId){
        Mail m = new Mail();
        m.setContent(content);
        m.setCreateDate(DateUtil.now());
        m.setReceiver(dao.get(User.class, userId));
        m.setStatus(Mail.MAIL_STATUS_NEW);
        m.setTitle(title);
        m.setSender(admin());
        m.setSenderRemove(true);
        m.setSenderDelete(true);
        dao.insertOrUpdate(m);
    }

    public void changeStatus(Mail mail, int mailStatus){
        dao.load(mail);
        mail.setStatus(mailStatus);
        dao.update(mail);
    }

    public void deletMail(Mail mail){
        dao.load(mail);
        if (mail.getSender().getId().equals(WebOptUtil.curUser().getId())) {
            if (mail.isReceiverDelete()) {
                dao.delete(mail);
            } else {
                mail.setSenderDelete(true);
                dao.update(mail);
            }
        } else {
            if (mail.isSenderDelete()) {
                dao.delete(mail);
            } else {
                mail.setReceiverDelete(true);
                dao.update(mail);
            }
        }
    }

    public void remove(Mail mail){
        dao.load(mail);
        if (mail.getSender().getId().equals(WebOptUtil.curUser().getId())) {
            mail.setSenderRemove(true);
        } else {
            mail.setReceiverRemove(true);
        }
        mail.setStatus(Mail.MAIL_STATUS_DELETE);
        dao.update(mail);
    }

    public void reset(Mail mail){
        dao.load(mail);
        mail.setStatus(Mail.MAIL_STATUS_READ);
        if (mail.getSender().getId().equals(WebOptUtil.curUser().getId())) {
            mail.setSenderDelete(false);
            mail.setSenderRemove(false);
        } else {
            mail.setReceiverDelete(false);
            mail.setReceiverRemove(false);
        }
        dao.update(mail);
    }

}
